<?php

class Testing extends MY_Controller {

	function __construct() {
		parent::__construct();
	}
	
	public function index() {
		$update_time = date('Y-m-d H:i:s');
		$facebook = new Facebook();
		
	
		$this->load->model('emailgenerator_model');
		$this->load->model('user_model');
		
		$user_with_newsletter = $this->emailgenerator_model->get_user_with_newsletter();
		$i = 0;
		foreach ($user_with_newsletter as $user)  {

			$time_diff = strtotime($update_time) - strtotime($user['last_newsletter']);
			echo $i++ . "<br />";
			//echo $time_diff . "<br />";
			//echo ($time_diff/24/60/60) . "<br />";
			$fb_account = $facebook->api('/' . $user['fb_id'],'GET');

			if ($user['newsletter_day'] && ($time_diff > $user['newsletter_day']*24*60*60)) {
				// At the beggining of the email
				$message = '<html>
					<body>
						<p> Dear ' . $fb_account['name'] . ', 
						</p>
						
						<p> Here are the latest updates from  
							<a href="'.base_url().'">The Trove</a>. 
						</p><br />';
				// New Offers	
				if ($user['new_offer']) {
					$new_offer = $this->emailgenerator_model->get_new_offer($user['last_newsletter']);
					if (count($new_offer) == 0) {
						$message .= '<p style="font-weight: bold"> No New Available Offers in the Trove.</p> <br/>';
					} else {
						$message .= '<p style="font-weight: bold; text-decoration: underline; "> ' . count($new_offer) . ' New Available Offer(s) in the Trove</p>';
						$message .= '<ul>';
						foreach($new_offer as $offer) {
							$message .= '<li><a href="'.base_url('/itemdescription/'.$offer['offer_id']) . '">' . $offer['item_title'] . '  (';
							if ($offer['item_price'] == 0) {
								$message .= 'Free';
							} else {
								$message .= '$' . $offer['item_price'] ;
							}
							$message .= ')</a></li>'; 
						}
						$message .= '</ul><br/>';
					}
				}
				
				// New Requesters		
				if ($user['new_request']) {
					$new_request = $this->emailgenerator_model->get_new_request($user['fb_id'], $user['last_newsletter']);
					if($new_request['total'] == 0) {
						$message .= '<p style="font-weight: bold"> You have no new requests for your Offers.</p> <br />';
					} else {
						$message .= '<p style="font-weight: bold; text-decoration: underline;">' . $new_request['total'] . ' new User(s) requesting for your Offers</p>';
						$message .= '<ul>';
						foreach ($new_request['offer'] as $offer) {
							if (count($offer['requester']) == 0) {
								$message .= '<li> No new User requested for <a href="'.base_url('/itemdescription/'.$offer['offer_id']) . '">' . $offer['item_title'] . '</a>. ';
							} else {
								$message .= '<li>' . count($offer['requester']) . ' new User(s) requested for <a href="'.base_url('/itemdescription/'.$offer['offer_id']) . '">' . $offer['item_title'] . '</a>: '; 
								foreach ($offer['requester']  as $req) {
									$temp_fb_acc = $facebook->api('/' . $req['fb_id'],'GET');
									$message .= $temp_fb_acc['name'] . ", " ;
								}
								$message[strlen($message)-2] = ".";
								$message .= '</li>'; 	
							}
						}
						$message .= '</ul><br />';
					}
				}
				
				// New Comments		
				if ($user['new_comment']) {
					$new_comment = $this->emailgenerator_model->get_new_comment($user['fb_id'], $user['last_newsletter']);
					if($new_comment['total'] == 0) {
						$message .= '<p style="font-weight: bold"> You have no new comments for any of your Offers.</p><br />';
					}
					else{
						$message .= '<p style="font-weight: bold; text-decoration: underline;">' . $new_comment['total'] . ' new comment(s) for your Offers</p>';
						$message .= '<ul>';
						foreach ($new_comment['offer'] as $offer) {
							$message .= '<li>' . count($offer['comment']) . ' new comment(s) for <a href="'.base_url('/itemdescription/'.$offer['offer_id']) . '">' . $offer['item_title'] . '</a> '; 
							$message .= '</li>'; 
							
						}
						$message .= '</ul><br />';
					}
				
				}
				// New Notifications
				if ($user['new_notification']) {
					$new_notification = $this->emailgenerator_model->get_new_notification($user['fb_id'], $user['last_newsletter']);
					if (count($new_notification) == 0) {
						$message .= '<p style="font-weight: bold"> You have no new <a href="'.base_url('/notifications/').'">notifications</a> from The Trove!</p> <br />';
					} else {
						$message .= '<p style="font-weight: bold;"> You have ' . count($new_notification) . ' new <a href="'.base_url('/notifications/').'">notification(s)</a> from The Trove!</p> <br />';
					}
				}		
				
				// End of email
				$message .=	'<p> View all your lastest updates in <a href="'.base_url().'"> The Trove </a> now! <br /> <br />				
								Note: This is a computer generated message. Please do not reply to this email.  <br />
								If you do not wish to receive any newsletter from The Trove, head to your 
									<a href="' . base_url('/profile/' . $user['fb_id']) . '">Profile</a> 
								page and modify your Newsletter preferences.
						</p> <br /> ';
				$message .= '</body>';
				$message .= '</html>';
				
				// Send email
				echo $message;
				$this->email_generator_model->insert_email($user['fb_id'], $message)
				//$this->user_model->update_last_newsletter($user['fb_id'], $update_time);
			} 
		}	
	}
	
}